package com.neusoft.ecommerce.controller;

import com.neusoft.ecommerce.dbhelper.LayuiPager;
import com.neusoft.ecommerce.entity.Manager;
import com.neusoft.ecommerce.entity.Storage;
import com.neusoft.ecommerce.service.ManagerService;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 紫煦桉
 */
@RestController
public class ManagerController {
    private final ManagerService managerService;

    public ManagerController(ManagerService managerService) {
        this.managerService = managerService;
    }

    @GetMapping("/manager_page")
    public ModelAndView managerPage() {
        return new ModelAndView("manager/manager-list");
    }

    @RequestMapping("/manager/list")
    @ResponseBody
    public LayuiPager<Manager> list(@RequestParam(value = "page", defaultValue = "1") String page,
                                    @RequestParam(value = "limit", defaultValue = "10") String limit) {
        System.out.println(page + "" + limit);
        LayuiPager<Manager> pager = new LayuiPager<>(Integer.parseInt(page), Integer.parseInt(limit));
        int count = managerService.findTotal(pager);
        List<Manager> list = managerService.findList(pager);
        for (Manager manager : list) {
            System.out.println(manager);
        }
        pager.setData(list);
        pager.setCount(count);
        pager.setCode(0);
        pager.setMsg("");
        return pager;
    }

    @GetMapping("/addManager")
    public ModelAndView addManager() {
        return new ModelAndView("manager/manager-add");
    }

    @PostMapping("/manager/add")
    public Map<String, Object> add(Manager manager) {
        System.out.println(manager);
        Map<String, Object> map = new HashMap<>(16);
        if (managerService.add(manager)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }

    @GetMapping("/manager/edit_page")
    public ModelAndView editPage(Manager manager, Model model) {
        System.out.println(manager);
        LayuiPager<Manager> pager = new LayuiPager<>(1,1);
        pager.setCondition(manager);
        Manager message = managerService.findList(pager).get(0);
        System.out.println(message);
        ModelAndView view = new ModelAndView("manager/manager-edit");
        model.addAttribute("manager", message);
        return view;
    }


    @PostMapping("/manager/edit")
    public Map<String, Object> edit(Manager manager) {
        System.out.println(manager);
        Map<String, Object> map = new HashMap<>(16);
        if (managerService.edit(manager)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }
    
    
    @PostMapping("/manager/delete")
    public Map<String, Object> delete(Manager manager) {
        System.out.println(manager);
        Map<String, Object> map = new HashMap<>(16);
        if (managerService.delete(manager)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }
    
    


}
